<?php
namespace App\Http\Service;

use App\Models\Category;

class CategoryService extends BaseService
{
	public function __construct()
	{
		parent::__construct();
	}

	public function getOneByIdUserId($id, $userId, $field = ['*'])
    {
        return Category::model()->simpleFindOne(['id' => $id, 'user_id' => $userId], $field);
    }

    public function getOneByUserIdCategoryName($userId, $categoryName, $field = ['*'])
    {
        return Category::model()->simpleFindOne(['user_id' => $userId, 'category_name' => $categoryName], $field);
    }

    public function getNoDefaultCategoryListByUserId($userId, $field = ['*'])
    {
        return Category::model()->where('user_id',$userId)->where('sort','!=',0)->get($field);
    }

    public function getCountByUserId($userId)
    {
        return Category::model()->where('user_id',$userId)->count();
    }

    /**
     * 获取列表
     * @param array $where
     * @param int $offset
     * @param int $limit
     * @param string[] $field
     * @return mixed
     */
    public function getCategoryList($where = [], $offset = 0, $limit = 10, $field = ['*'])
    {
        // order : sort asc,id asc
        return Category::model()->where($where)->offset($offset)->limit($limit)->orderBy('sort', 'asc')->orderBy('id', 'asc')->get($field);
    }

}
